# _*_coding:utf-8_*_
# @Author: VVcat
# @Time: 2019/10/6 11:03
# @File: LotteryWebsite.py
# @IDE: PyCharm
# @Email:
# @Version: 1.0
import gevent
import xlwt as xlwt
from lxml import etree
import requests

# 双色球


def two_color_ball():
    xls = xlwt.Workbook()
    sheet = xls.add_sheet("Two-color_ball", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/ssq-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        tail_number = ul.xpath("li/a/span[@class='ball lblue']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        sheet.write(row, 4, number[3])
        sheet.write(row, 5, number[4])
        sheet.write(row, 6, number[5])
        sheet.write(row, 7, tail_number[0])
        row += 1
        xls.save("Two-color_ball.xls")

# 福彩3D


def welfare_lottery_3d():

    xls = xlwt.Workbook()
    sheet = xls.add_sheet("Welfare_lottery_3D.xls", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/sd-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        row += 1
        xls.save("Welfare_lottery_3D.xls")

# 大乐透


def big_pleasure_lottery():

    xls = xlwt.Workbook()
    sheet = xls.add_sheet("big_pleasure_lottery", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/dlt-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        tail_number = ul.xpath("li/a/span[@class='ball lblue']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        sheet.write(row, 4, number[3])
        sheet.write(row, 5, number[4])
        sheet.write(row, 6, tail_number[0])
        sheet.write(row, 7, tail_number[1])
        row += 1
        xls.save("big_pleasure_lottery.xls")

# 排列三


def permutation_three():
    xls = xlwt.Workbook()
    sheet = xls.add_sheet("permutation_three", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/p3-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        row += 1
        xls.save("permutation_three.xls")

# 排列五


def permutation_five():

    xls = xlwt.Workbook()
    sheet = xls.add_sheet("permutation_five", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/p5-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        sheet.write(row, 4, number[3])
        sheet.write(row, 5, number[4])
        row += 1
        xls.save("permutation_five.xls")

# 七星彩


def seven_star_color():

    xls = xlwt.Workbook()
    sheet = xls.add_sheet("seven_star_color", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/qxc-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        sheet.write(row, 4, number[3])
        sheet.write(row, 5, number[4])
        sheet.write(row, 6, number[5])
        sheet.write(row, 7, number[6])
        row += 1
        xls.save("seven_star_color.xls")

# 七乐彩


def seven_music_lottery():

    xls = xlwt.Workbook()
    sheet = xls.add_sheet("seven_music_lottery", cell_overwrite_ok=True)
    row = 0
    url = "http://m.55123.cn/kjh/qlc-history-120.htm"
    req = requests.get(url)
    resp = req.content.decode("utf-8")
    html = etree.HTML(resp)
    uls = html.xpath("/html/body/div[@class='wrap']/div[@class='lottery']/ul")
    for ul in uls:
        date = ul.xpath("li/span[@class='dates']")
        number = ul.xpath("li/a/span[@class='ball lred']/text()")
        tail_number = ul.xpath("li/a/span[@class='ball lblue']/text()")
        sheet.write(row, 0, date[0].text)
        sheet.write(row, 1, number[0])
        sheet.write(row, 2, number[1])
        sheet.write(row, 3, number[2])
        sheet.write(row, 4, number[3])
        sheet.write(row, 5, number[4])
        sheet.write(row, 6, number[5])
        sheet.write(row, 7, number[6])
        sheet.write(row, 8, tail_number[0])
        row += 1
        xls.save("seven_music_lottery.xls")


if __name__ == '__main__':
    two_color_ball = gevent.spawn(two_color_ball)
    welfare_lottery_3d = gevent.spawn(welfare_lottery_3d)
    big_pleasure_lottery = gevent.spawn(big_pleasure_lottery)
    permutation_three = gevent.spawn(permutation_three)
    permutation_five = gevent.spawn(permutation_five)
    seven_star_color = gevent.spawn(seven_star_color)
    seven_music_lottery = gevent.spawn(seven_music_lottery)
    two_color_ball.join()
    welfare_lottery_3d.join()
    big_pleasure_lottery.join()
    permutation_three.join()
    permutation_five.join()
    seven_star_color.join()
    seven_music_lottery.join()
